#!/bin/bash
# BOTSOL macOS launcher — template. build-app.sh fills in BOTSOL Bot,
# BOTSOL Bot and index.js.
#
# Design rules from TECH_SPEC_V5 §2 and §7:
#   - The app NEVER writes inside its own bundle. All writable state (.env, the
#     SQLite db) lives in a per-user Application Support folder.
#   - A visible Terminal window, so a CLI trading bot is never a silent
#     invisible process.
#   - Node is detected across common install locations; if missing, the user is
#     guided to install it rather than met with a cryptic failure.

APP_NAME="BOTSOL Bot"
WORK_DIR="$HOME/Library/Application Support/BOTSOL Bot"
ENTRY="index.js"

# Resolve the bundle's own Resources/app directory (read-only).
HERE="$(cd "$(dirname "$0")" && pwd)"
APP_SRC="$(cd "$HERE/../Resources/app" && pwd)"

# ── Re-launch inside Terminal if double-clicked from Finder ──────────────────
# Finder runs this with no attached terminal. Detect that and relaunch visibly.
# BOTSOL_NO_TERMINAL=1 skips this — useful when already in a terminal, over SSH,
# or in CI, where opening Terminal.app is wrong or impossible.
if [ "${BOTSOL_NO_TERMINAL:-}" != "1" ] && [ ! -t 1 ]; then
  osascript >/dev/null 2>&1 <<OSA
tell application "Terminal"
  activate
  do script "'$HERE/$(basename "$0")'"
end tell
OSA
  exit 0
fi

echo "──────────────────────────────────────────────"
echo "  $APP_NAME"
echo "──────────────────────────────────────────────"
echo

# ── Node detection ───────────────────────────────────────────────────────────
find_node() {
  if command -v node >/dev/null 2>&1; then command -v node; return; fi
  for candidate in \
    /usr/local/bin/node \
    /opt/homebrew/bin/node \
    "$HOME/.nvm/versions/node"/*/bin/node \
    /usr/bin/node; do
    [ -x "$candidate" ] && { echo "$candidate"; return; }
  done
  return 1
}

NODE="$(find_node)"
if [ -z "$NODE" ]; then
  echo "Node.js was not found on this Mac."
  echo
  echo "  $APP_NAME needs Node.js 18 or newer to run."
  echo "  Install it from https://nodejs.org (the LTS build), then reopen this app."
  echo
  echo "Press any key to close."
  read -r -n 1
  exit 1
fi
echo "Using Node: $NODE ($("$NODE" --version))"

# ── Writable per-user working folder ─────────────────────────────────────────
mkdir -p "$WORK_DIR"

# First run: seed .env from the bundled example and open it for editing.
if [ ! -f "$WORK_DIR/.env" ]; then
  if [ -f "$APP_SRC/.env.example" ]; then
    cp "$APP_SRC/.env.example" "$WORK_DIR/.env"
  else
    touch "$WORK_DIR/.env"
  fi
  echo
  echo "First run — created your configuration file at:"
  echo "  $WORK_DIR/.env"
  echo
  echo "Opening it now. Fill it in, save, then reopen this app."
  open -e "$WORK_DIR/.env"
  echo "Press any key to close once you have saved your settings."
  read -r -n 1
  exit 0
fi

# ── Run ──────────────────────────────────────────────────────────────────────
# cwd is the WRITABLE folder, so dotenv reads WORK_DIR/.env and any file the app
# writes (e.g. the licence-server SQLite db via DB_PATH) lands outside the
# bundle. The app's own code is read from the read-only bundle.
cd "$WORK_DIR" || exit 1
export DB_PATH="$WORK_DIR/botsol.db"

echo
echo "Starting $APP_NAME. Leave this window open while it runs."
echo "Press Control-C to stop."
echo "──────────────────────────────────────────────"
echo
exec "$NODE" "$APP_SRC/$ENTRY"
